package buber.data;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


import buber.exceptions.BadDataException;
import buber.util.Const;

public class DataSet {
	private String id;
	private String name="name";
	private List<DataRow> rows;
	private List<String> names;
	private int numRows=0;
	private int numCols=0; 
	private boolean namesInFirstRow=false;
	private boolean rowNumbers=false;
	public DataSet(){
		rows = new ArrayList<DataRow>();
		names = new ArrayList<String>();
	}
	public DataSet(boolean namesInFirstRow,boolean rowNums){
		rows = new ArrayList<DataRow>();
		names = new ArrayList<String>();
		this.namesInFirstRow=namesInFirstRow;
		this.rowNumbers=rowNums;
	}
	public void build(String dataString) throws BadDataException{
		try{
		StringTokenizer st=new StringTokenizer(dataString,Const.ROW_SEPARATOR);
		if(namesInFirstRow){
			setNames(st.nextToken());
		}
		while(st.hasMoreTokens()){
			String n=st.nextToken();
			if(rowNumbers){
				n=n.substring(n.indexOf(Const.COL_SEPARATOR));
			}
			rows.add(new DataRow(n));
			numRows++;
		}
		}catch(ArrayIndexOutOfBoundsException e){
			throw new BadDataException("build(String)");
		}
	}
	public void build(DataCell[][] array){
		for(int x=0;x<array.length;x++){
			DataRow row = new DataRow();
			for(int y=0;y<array[x].length;y++){
				row.addCell(array[y][x]);
			}
			rows.add(row);
		}
	}
	public DataCell[][] asCellArray() throws BadDataException{
		DataCell[][] array=new DataCell[rows.get(0).getSize()][rows.size()];
		try{
		for(int y=0;y<rows.size();y++){
			for(int x=0;x<rows.get(y).getCells().size();x++){
				array[x][y]=rows.get(y).getCells().get(x);
			}
		}
		}catch(ArrayIndexOutOfBoundsException e){
			throw new BadDataException("asCellArray()");
		}
		return array;
	}
	public String asString(){
		StringBuffer sb = new StringBuffer();
		for(DataRow row:rows){
			for(DataCell cell:row.getCells()){
				sb.append(cell.getValue());
				sb.append(Const.COL_SEPARATOR);
			}
			sb.append(Const.ROW_SEPARATOR);
		}
		return sb.toString();
	}
	public int getNumRows() {
		return numRows;
	}
	public void setNumRows(int numRows) {
		this.numRows = numRows;
	}
	public int getNumCols() {
		return numCols;
	}
	public void setNumCols(int numCols) {
		this.numCols = numCols;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<DataRow> getRows() {
		return rows;
	}
	public void setRows(List<DataRow> rows) {
		this.rows = rows;
	}

	public List<String> getNames() {
		return names;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}
	public void setNames(String namesString) throws BadDataException{
		StringTokenizer st=new StringTokenizer(namesString,Const.COL_SEPARATOR);
		while(st.hasMoreTokens()){
			names.add(st.nextToken());
		}
	}
	public boolean isNamesInFirstRow() {
		return namesInFirstRow;
	}
	public void setNamesInFirstRow(boolean namesInFirstRow) {
		this.namesInFirstRow = namesInFirstRow;
	}
	public boolean isRowNumbers() {
		return rowNumbers;
	}
	public void setRowNumbers(boolean rowNumbers) {
		this.rowNumbers = rowNumbers;
	}
}
